package com.xysoft.framework.mvc;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;

public class AliasHandlerMapping extends SimpleUrlHandlerMapping {

	@Override
	protected void registerHandlers(Map<String, Object> urlMap) throws BeansException {
		if (urlMap.isEmpty()) {
			logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
		}
		else {
			for (Map.Entry<String, Object> entry : urlMap.entrySet()) {
				String alias = entry.getKey();
				Object handler = entry.getValue();
				if (!alias.startsWith("/")) {
					alias = "/" + alias;
				}
				
				if(!alias.endsWith("/**")) {
					alias = alias + "/**";
				}
				if (handler instanceof String) {
					handler = ((String) handler).trim();
				}
				registerHandler(alias, handler);
			}
		}
	}
	
	
}
